
#include <config.h>
#include <input_manager.h>
#include <string.h>
#include <sys/select.h>
#include <sys/types.h>
#include <unistd.h>

static PT_InputOpr g_ptInputOprHead;
static int g_iMAXfd = -1;
static pthread_mutex_t g_tMutex = PTHREAD_MUTEX_INITIALIZER;
static pthread_cond_t g_tCond = PTHREAD_COND_INITIALIZER;
static T_InputEvent g_tInputEvent;


int RegisterInputOpr(PT_InputOpr ptInputOpr)
{
	PT_InputOpr ptTmp;

	if (!g_ptInputOprHead)
	{
		g_ptInputOprHead   = ptInputOpr;
		ptInputOpr->ptNext = NULL;
	}
	else
	{
		ptTmp = g_ptInputOprHead;
		while (ptTmp->ptNext)
		{
			ptTmp = ptTmp->ptNext;
		}
		ptTmp->ptNext	  = ptInputOpr;
		ptInputOpr->ptNext = NULL;
	}

	return 0;
}


void ShowInputOpr(void)
{
	int i = 0;
	PT_InputOpr ptTmp = g_ptInputOprHead;

	while (ptTmp)
	{
		printf("%02d %s\n", i++, ptTmp->name);
		ptTmp = ptTmp->ptNext;
	}
}
void * InputEventTreadfun(void * Pvoid)
{
	T_InputEvent tInputEvent;
		
	int (*GetInputEvent)(PT_InputEvent ptInputEvent);
	GetInputEvent = (int (*) (PT_InputEvent))Pvoid;
	
	while(1)
	{
		if(0 == GetInputEvent(&tInputEvent))
		{
			//获得输入时间  唤醒主线程
			pthread_mutex_lock(&g_tMutex);
			
			g_tInputEvent = tInputEvent;

			pthread_cond_signal(&g_tCond);
			pthread_mutex_unlock(&g_tMutex);
		}
	}
	return NULL;
}
int AllInputDevicesInit(void)
{
	PT_InputOpr ptTmp = g_ptInputOprHead;
	int iError = -1;

	while (ptTmp)
	{
		if (0 == ptTmp->DeviceInit())
		{	
			//创建子线程
			pthread_create(&ptTmp->tTreadId,NULL,InputEventTreadfun,ptTmp->GetInputEvent);
			iError = 0;
		}
		ptTmp = ptTmp->ptNext;
	}
	g_iMAXfd++;
	return iError;
}

int GetInputEvent(PT_InputEvent ptInputEvent)
{
	/*休眠   被唤醒后返回数据*/
	pthread_mutex_lock(&g_tMutex);

	pthread_cond_wait(&g_tCond,&g_tMutex);

	*ptInputEvent = g_tInputEvent;
	pthread_mutex_unlock(&g_tMutex);
	return 0;
}

int InputInit(void)
{
	int iError;
	iError = StdinInit();
	iError |= TouchScreenInit();
	return iError;
}

